#include "serializer/flatbuffer_serializer.h"
#include <cstring>
#include <flatbuffers/flatbuffers.h>
#include <flatbuffers/verifier.h>


template<typename DataType>
std::vector<uint8_t> FlatBufferSerializer::serialize(const DataType& data) {
    flatbuffers::FlatBufferBuilder builder;
    auto fb_offset = flatbuffers::serialize(builder, data);
    builder.Finish(fb_offset);
    return std::vector<uint8_t>(builder.GetBufferPointer(), builder.GetBufferPointer() + builder.GetSize());
}

template<typename DataType, typename FBType>
bool FlatBufferSerializer::deserialize(const std::vector<uint8_t>& serialized_data, DataType& output_data) {
    const FBType* fb_obj = flatbuffers::GetRoot<FBType>(serialized_data.data());
    if (!fb_obj) {
        return false;
    }
    return flatbuffers::deserialize(output_data, fb_obj);
}
